/*
 * Copyright (C) 2004, Velocityme Partnership
 * ContactDetailTypeActions.java
 *
 * Created on 16 October 2004, 10:14
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.session.ContactDetailLocal;
import com.velocityme.session.KeyLocal;
import com.velocityme.www.actionforms.ContactDetailTypeActionForm;
import com.velocityme.www.utility.NodeUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class ContactDetailTypeActions extends NodeActions {

    /** Creates a new instance of ContactDetailTypeActions */
    public ContactDetailTypeActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("selectedTab", "ContactDetailTypes");
        return super.show(mapping, form, request, response);
    }

    @Override
    public ActionForward showNode(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("contactDetailTypes", lookupContactDetailBean().findAllContactDetailTypes(key));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        ContactDetailTypeActionForm actionForm = (ContactDetailTypeActionForm)form;
        actionForm.setCmd(0,"");
        actionForm.setContactDetailTypes(lookupContactDetailBean().findAllContactDetailTypes(key));
        session.setAttribute("notificationPairs", NodeUtil.getAllNotificationPairs(key));
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        ContactDetailTypeActionForm actionForm = (ContactDetailTypeActionForm)form;
        if (actionForm.processCmd()) {
            actionForm.setCmd(0,"");
            return mapping.findForward("ACTION_EDIT");
        }
        if (request.getParameter("btnSubmit").equals("OK")) {
            lookupContactDetailBean().editContactDetailTypes(key, actionForm.getContactDetailTypes());
        }
        Long nodeId = (Long) session.getAttribute("nodeId");
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private ContactDetailLocal lookupContactDetailBean() {
        try {
            Context c = new InitialContext();
            return (ContactDetailLocal) c.lookup("java:comp/env/ContactDetailBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
